var dateCookieSelect = 'kejet_marketplace_select_date';
var dateCookieInput = 'kejet_marketplace_input_date';
$(function(){
    $("#txtDP").click(function(){
        creatDP($(this));
    });
    $("#selDP").change(function(){
        var changeVal = $(this).val();
        SetCookie(dateCookieSelect, changeVal);

        //选择自定义时间
        if(changeVal==1){
            $("#conDP").remove();
            creatDP($("#txtDP"));
        }else{
            $("#conDP").remove();
            //选择全部时间
            if(changeVal==0){
                $("#txtDP").val("全部时间");
                SetCookie(dateCookieInput, "全部时间");
            }
            //选择下拉中的 最近时间
            else{
                $("#txtDP").val(changeVal);
                SetCookie(dateCookieInput, changeVal);
            }
            getData();
        }
    });
    var dateCookieSelectVal = GetCookie(dateCookieSelect);
    if(dateCookieSelectVal){
        $("#selDP option").each(function(){
            if($(this).val() == dateCookieSelectVal){
                $(this).attr("selected","true");

                //避免 cookie中是 全部时间 而当前 没有 全部时间
                var dateCookieInputVal = GetCookie(dateCookieInput);
                if(dateCookieInputVal){
                    $("#txtDP").val(dateCookieInputVal);
                }
            }
            //else 如果不存在 cookie中的时间 则用自己页面 默认时间
        });
    }
    getData();
});
function creatDP(o){
    o.after('<div id="conDP" class="dPick"><div class="clr"><div id="sdate" name="sdate" class="fl"></div><div id="edate" name="edate" class="fr"></div></div><div class="sded"><span>开始日期：<input id="sd" type="text" /></span>结束日期：<input id="ed" type="text" /></div><div class="cfb"><input id="subdate" type="button" value="" class="vm hand" /><a id="closedate" class="b" href="javascript:;">返回</a></div></div>');
    $("#conDP").css("top",o.position().top+o.outerHeight()+1);
    $("#conDP").css("left",o.position().left-185);
    $("#sdate").datepicker({
        "onSelect":function(d){
            $("#edate").datepicker("option","minDate",d);
            freshDate();
        }
    });
    $("#edate").datepicker({
        "onSelect":function(d){
            $("#sdate").datepicker("option","maxDate",d);
            freshDate();
        }
    });
    if(o.val().indexOf("-")>=0){
        var d = o.val().split(" - ");
        $("#sdate").datepicker("setDate",d[0]);
        $("#edate").datepicker("setDate",d[1]);
    }
    freshDate();
    $("#closedate").click(function(){
        $("#conDP").remove();
    });
    $("#subdate").click(function(){
        $("#selDP").val(1);
        $("#selDP option").each(function(i,v){
            if($(this).val() == freshDate()){
                $("#selDP").val(freshDate());
            }
        });
        $("#txtDP").val(freshDate());
        $("#conDP").remove();
        SetCookie(dateCookieInput, $("#txtDP").val());
        getData();
    });
}
function freshDate(){
    var vsd = $.datepicker.formatDate("yy-mm-dd",$("#sdate").datepicker("getDate"));
    var ved = $.datepicker.formatDate("yy-mm-dd",$("#edate").datepicker("getDate"));
    $("#sd").val(vsd);
    $("#ed").val(ved);
    return $("#sd").val()+" - "+$("#ed").val();
}


//设定Cookie值
function SetCookie(name, value)
{
    var expdate = new Date();
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = new Date();
    expires.setTime(expires.getTime() + 356*86400*1000); //60秒
    var path = '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expires.toGMTString()))
    +((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
    +((secure == true) ? "; secure" : "");
}

//删除Cookie
function DelCookie(name)
{
    name = user_id+name;
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = GetCookie (name);
    document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}

//获得Cookie解码后的值
function GetCookieVal(offset)
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

//获得Cookie的原始值
function GetCookie(name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var tmp = '';
    while (i < clen)
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
        tmp = GetCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return tmp;
}

function getData(){
    $("#search_btn").click();
}